package com.dmx.platform.dao.impl;

import java.util.List;

import com.dmx.fw.dao.hibernateimpl.BaseDaoImpl;
import com.dmx.platform.dao.InformationDao;
import com.dmx.platform.entity.TBaseinfo;
import com.dmx.platform.entity.TDetailinfo;
import com.dmx.platform.entity.TPricePlan;

/**
 *@author: Beni Yang
 *@date:2012-8-18
 *@function:
 */
public class InformationDaoImpl extends BaseDaoImpl implements InformationDao {

    public void batchDeleteBaseInfoByIds(String[] ids){
    	if(ids==null){
    		throw new IllegalArgumentException("arg ids couldn't be null for UserInfoDaoImpl.batchDeleteUserInfoByIds");
    	}
        if (ids != null) {
            for(String id:ids){
            	deleteBaseInfoById(id);
            }
        }
    }

    public void deleteBaseInfoById(String id){
    	TBaseinfo baseInfo =findBaseInfoById(id);
    	if(baseInfo!=null)
    		this.getHibernateTemplate().delete(baseInfo);
    }

    public void saveOrUpdateBaseInfo(TBaseinfo info){
    	this.getHibernateTemplate().saveOrUpdate(info);
    }

    @SuppressWarnings("unchecked")
	public TBaseinfo findBaseInfoById(String id){
    	if(id==null||"".equals(id)){
    		throw new IllegalArgumentException("arg id couldn't be null for UserInfoDaoImpl.deleteUserInfoById");
    	}
        String hql = "from TBaseinfo where baseId=?";
        Integer baseid=Integer.parseInt(id);
        List<TBaseinfo> infos = this.getHibernateTemplate().find(hql,baseid);
        if(!infos.isEmpty())
          return infos.get(0);
        return null;
    }

	@Override
	public void saveOrUpdateDetailInfo(TDetailinfo detailInfo) {
		this.getHibernateTemplate().saveOrUpdate(detailInfo);
		
	}

	@Override
	public List<TPricePlan> findPricePlan(Integer ownerId) {
        String hql = "from TPricePlan where ownerId=?";
        List<TPricePlan> infos = this.getHibernateTemplate().find(hql,ownerId);
        return infos;
	}

	@Override
	public boolean deletePricePlanByOwnerId(Integer ownerId) {
		List<TPricePlan> plans=findPricePlan(ownerId);
		if(!plans.isEmpty()){
			this.getHibernateTemplate().deleteAll(plans);
		}
		return true;
		
	}

	@Override
	public boolean saveOrUpdatePricePlan(List<TPricePlan> priceList) {
		if(!priceList.isEmpty()){
			Integer ownerId=priceList.get(0).getOwnerId();
			if(deletePricePlanByOwnerId(ownerId)){
				this.getHibernateTemplate().saveOrUpdateAll(priceList);
				return true;
			}
		}
		return false;
		
	}

	



	
	
}
